/* 
 * Distributed as part of jdiff
 * 
 * Copyright (C) 2008 QArks.com
 *
 * Author: Pierre Meyer <support@qarks.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1, as 
 * published by the Free Software Foundation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; see the file LICENSE.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package diff.tela;

import java.awt.*;
import javax.swing.*;

import diff.misc.*;

public class LineCellRenderer extends DefaultListCellRenderer {

	public static Color MOVED_COLOR = new Color(134, 156, 234);
	public static Color INSERTED_COLOR = new Color(156, 250, 158);
	public static Color DELETED_COLOR = new Color(249, 113, 113);
	public static Color MODIFIED_COLOR = new Color(252, 177, 109);
	public static Color CONFLICT_COLOR = DELETED_COLOR;
	public static Color WARNING_COLOR = MODIFIED_COLOR;
	private boolean showNoMatchAsAdded;

	public LineCellRenderer(boolean showNoMatchAsAdded) {
		setOpaque(true);
		this.showNoMatchAsAdded = showNoMatchAsAdded;
	}

	public Component getListCellRendererComponent(
			JList list,
			Object value,
			int index,
			boolean isSelected,
			boolean cellHasFocus) {

		if (value instanceof FileLine) {
			FileLine line = (FileLine) value;

			int lineIndex = line.getIndex();
			if (lineIndex > -1) {
				switch (line.getStatus()) {
				case FileLine.NO_MATCH:
					if (showNoMatchAsAdded) {
						if (isSelected) {
							setBackground(INSERTED_COLOR.darker());
						}
						else {
							setBackground(INSERTED_COLOR);
						}
					}
					else {
						if (isSelected) {
							setBackground(DELETED_COLOR.darker());
						}
						else {
							setBackground(DELETED_COLOR);
						}
					}
					break;
				case FileLine.DELETED_ON_OTHER_SIDE:
					if (isSelected) {
						setBackground(DELETED_COLOR.darker());
					}
					else {
						setBackground(DELETED_COLOR);
					}
					break;
				case FileLine.INSERTED_ON_OTHER_SIDE:
					if (isSelected) {
						setBackground(INSERTED_COLOR.darker());
					}
					else {
						setBackground(INSERTED_COLOR);
					}
					break;
				case FileLine.MOVED:
					if (isSelected) {
						setBackground(MOVED_COLOR.darker());
					}
					else {
						setBackground(MOVED_COLOR);
					}
					break;
				case FileLine.MODIFIED:
					if (isSelected) {
						setBackground(MODIFIED_COLOR.darker());
					}
					else {
						setBackground(MODIFIED_COLOR);
					}
					break;
				default:
					if (isSelected) {
						setBackground(Color.lightGray.brighter());
					}
					else {
						setBackground(Color.white);
					}
				break;
				}
				int idx = line.getIndex();
				setText(idx + "  " + line.getContent());
			}
			else {
				setBackground(Color.lightGray);
				setText("-");
			}
		}
		return this;
	}
}
